package cn.edu.scau.cmi.wujiaxian.exercise10;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.Scanner;


public class Example10_14 {
	
	//对象锁
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		File file = new File("C:\\Users\\lenovo\\git\\201734020124_wu_jiaxian\\201734020124吴嘉贤\\src\\cn\\edu\\scau\\cmi\\wujiaxian\\exercise10\\Example10_14.java");
		Scanner scanner = new Scanner(System.in);
		try {
			RandomAccessFile input = new RandomAccessFile(file, "rw");
			FileChannel channel = input.getChannel();
			FileLock lock = channel.tryLock();
			while (scanner.hasNextInt()) {
				int m = scanner.nextInt();
				lock.release();
				for (int i = 1; i <= m; i++) {
					String line = input.readLine();
					System.out.println(line);
				}
				lock = channel.tryLock();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
}
